package com.bobo.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobo.rbac.domain.Role;
import com.bobo.rbac.domain.RoleMenu;
import com.bobo.rbac.service.RoleMenuService;
import com.bobo.rbac.mapper.RoleMenuMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author bobo
* @description 针对表【role_menu】的数据库操作Service实现
* @createDate 2022-07-27 14:42:55
*/
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
    implements RoleMenuService{

    @Override
    public boolean bindMenu(Role role) {
        //1先删除已有角色菜单
        QueryWrapper<RoleMenu> qw = new QueryWrapper<>();
        qw.eq("rid",role.getId());
        this.remove(qw);
        //2.封装roleMenu 对象，并执行批量添加角色菜单
        List<Integer> mids = role.getMids();
        List<RoleMenu> list = new ArrayList<>();
        if(null!=mids){
             mids.forEach(mid->{
                 RoleMenu roleMenu = new RoleMenu();
                 roleMenu.setMid(mid);
                 roleMenu.setRid(role.getId());
                 list.add(roleMenu);
             });

         }
        return  this.saveBatch(list);
    }
}




